﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using FluentValidation.Attributes;
using FluentValidation.Mvc;
using FluentValidation.Validators;
using Nop.Web.Framework.Mvc;
using Nop.Web.Validators.Boards;
using SDF.Core.Infrastructure;
using StackExchange.Profiling;

namespace SDFAuth.Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Login", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

        }

        protected void Application_Start()
        {
            EngineContext.Initialize(false);
            //set dependency resolver
            DependencyResolver.SetResolver(new SDFDependencyResolver());

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);


            //Validator
            DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
          
            //ModelValidatorProviders.Providers.Add(
            //                new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));
            //FluentValidationModelValidatorProvider.Configure(provider =>
            //{
            //    provider.ValidatorFactory = new MyCustomValidatorFactory();
            //});
            FluentValidationModelValidatorProvider.Configure(x =>
            {
                x.Add(typeof(LessThanOrEqualValidator), (metadata, context, rule, validator) => new LessThanOrEqualToFluentValidationPropertyValidator(metadata, context, rule, validator));
            });
            //MiniProfile
            MiniProfilerEF.Initialize(); 

        }
    }
}